package com.ruoyi.web.controller.ydz;

import java.util.List;

import com.ruoyi.web.domain.Drug;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.Doctor;
import com.ruoyi.web.service.IDoctorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医生Controller
 * 
 * @author why
 * @date 2025-05-13
 */
@Controller
@RequestMapping({"/ydz/doctor","/api/doctor"})
@Api(tags = "医生")
public class DoctorController extends BaseController
{
    private String prefix = "ydz/doctor";

    @Autowired
    private IDoctorService doctorService;

    @RequiresPermissions("ydz:doctor:view")
    @GetMapping()
    public String doctor()
    {
        return prefix + "/doctor";
    }

    /**
     * 查询医生列表
     */
    @RequiresPermissions("ydz:doctor:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询医生列表")
    public TableDataInfo list(Doctor doctor)
    {
        startPage();
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        return getDataTable(list);
    }

    @PostMapping("/getDoctorList")
    @ResponseBody
    @ApiOperation("查询医生列表")
    public TableDataInfo getDoctorList(@RequestBody Doctor doctor)
    {
        startPage();
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        return getDataTable(list);
    }

    /**
     * 导出医生列表
     */
    @RequiresPermissions("ydz:doctor:export")
    @Log(title = "医生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation("导出医生列表")
    public AjaxResult export(Doctor doctor)
    {
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        return util.exportExcel(list, "医生数据");
    }

    /**
     * 新增医生
     */
    @GetMapping("/add")
    @ApiOperation("新增医生")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医生
     */
    @ApiOperation("新增保存医生")
    @RequiresPermissions("ydz:doctor:add")
    @Log(title = "医生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.insertDoctor(doctor));
    }

    /**
     * 修改医生
     */
    @ApiOperation("修改医生")
    @RequiresPermissions("ydz:doctor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Doctor doctor = doctorService.selectDoctorById(id);
        mmap.put("doctor", doctor);
        return prefix + "/edit";
    }

    /**
     * 修改保存医生
     */
    @ApiOperation("修改保存医生")
    @RequiresPermissions("ydz:doctor:edit")
    @Log(title = "医生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.updateDoctor(doctor));
    }

    /**
     * 删除医生
     */
    @ApiOperation("删除医生")
    @RequiresPermissions("ydz:doctor:remove")
    @Log(title = "医生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(doctorService.deleteDoctorByIds(ids));
    }

    /*
    * 获取医生详情
    * */
    @ApiOperation("获取医生详情")
    @GetMapping("/getDoctorById/{id}")
    @ResponseBody
    public AjaxResult getDoctorById(@PathVariable("id") Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            Doctor doctor = doctorService.selectDoctorById(id);
            ajaxResult.put("data",doctor);
            ajaxResult.put("code",0);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取医生详情失败，请联系管理员！");
        }
        return ajaxResult;
    }
}
